import QrcodeGenerator from "./QrcodeGenerator";
import { buildSdk } from "./sdk";

export default async function YydsQrcodePage() {

    async function generateSDK(rescode, token) {
        "use server";

        const URL = "https://bdwy.zerom.cn/mini/index/get_rescode_param";

        // 时间取当前时间 - 5min，格式 yyMMddHHmm
        const now = new Date(Date.now() - 5 * 60 * 1000);
        const pad = (n) => n.toString().padStart(2, "0");
        const beginDate =
          pad(now.getFullYear() % 100) +
          pad(now.getMonth() + 1) +
          pad(now.getDate()) +
          pad(now.getHours()) +
          pad(now.getMinutes());

        const resp = await fetch(URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": token
          },
          body: `rescode=${rescode}&idx=0`,
          cache: "no-store",
        });

        const json = await resp.json();
        if (json.status !== 1) throw new Error("接口返回异常，请检查 Token 是否正确");
        console.log(json.param);
        json.param.beginDate = beginDate;
        return buildSdk(json);
      }

  return <QrcodeGenerator generateSDK={generateSDK} />;
}